package com.five.mobile.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.five.admin.threadLocal.UserThreadLocal;
import com.five.core.result.Result;
import com.five.core.result.ResultEnum;
import com.five.mobile.service.CarService;
import com.five.mobile.service.OrderService;

import com.five.pojo.dto.OrderCreateDto;
import com.five.pojo.entity.Order;

import com.five.pojo.vo.OrderListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/userOrder")
public class OrderController {
    @Resource
    private OrderService orderService;

    @Resource
    private CarService carService;

    //查询当前用户的订单分页
    @PostMapping("/getUserOrders")
    public Result getUserOrders(Integer pageNum, Integer pageSize) {
        Long id = UserThreadLocal.get().getId();
        Page<OrderListVO> page = orderService.getUserOrders(id, pageNum, pageSize);
        return page!=null?Result.buildSuccess(page.getTotal(),page.getRecords()):Result.buildFail(ResultEnum.DATA_IS_NULL);
    }

    //取消订单
    @PostMapping("/cancelOrder")
    public Result cancelOrder(Long orderId, String cancelReason) {
        boolean cancel = orderService.cancelOrder(orderId, cancelReason);
        return cancel?Result.buildSuccess():Result.buildFail(ResultEnum.OPERATE_FAIL);
    }

    //支付订单
    @PostMapping("/payOrder")
    public Result payOrder(String orderNo, Integer payType) {
        boolean pay = orderService.payOrder(orderNo, payType);
        return pay?Result.buildSuccess():Result.buildFail(ResultEnum.OPERATE_FAIL);
    }

    //确认收货
    @PostMapping("/confirmReceive")
    public Result confirmReceive(Long orderId) {
        boolean confirm = orderService.confirmReceive(orderId);
        return confirm?Result.buildSuccess():Result.buildFail(ResultEnum.OPERATE_FAIL);
    }

    //获取订单详情(通过订单 id）
    @PostMapping("/getOrderDetail")
    public Result getOrderDetail(Long orderId) {
        return Result.buildSuccess(orderService.getOrderDetail(orderId));
    }

    //获取订单详情（通过订单详情
    @PostMapping("/getOrderDetailByNo")
    public Result getOrderDetailByNo(String orderNo) {
        return Result.buildSuccess(orderService.getOrderDetailByNo(orderNo));
    }

    @PostMapping("/createOrder")
    public Result createOrder(@Valid @RequestBody OrderCreateDto orderCreateDto) {  // 用@RequestBody接收JSON，@Valid开启校验
        Order order = new Order();
        BeanUtils.copyProperties(orderCreateDto,order);
        // 2. 调用服务层创建订
        Long id = UserThreadLocal.get().getId();
        order.setReceiverAddress(orderCreateDto.getAddress());

        order.setUserId(id);
        String order1 = orderService.createOrder(order, orderCreateDto.getOrderItems());
        // 3. 返回成功结果（包含订单关键信息）

        System.err.println(orderCreateDto.getCarIds());
        orderCreateDto.getCarIds().forEach(carId -> carService.deleteCarInfo(id,carId));
        return Result.buildSuccess(order1);
    }

}
